package com.example.wangzh.mytest.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * Created by zhwang16 on 2018/9/21.
 */

public class LayoutTestView extends LinearLayout {
    private static int DEFAULT_WIDTH = 400;
    private static int DEFAULT_HEIGHT = 800;
    //设置成ViewGroup.LayoutParams.WRAP_CONTENT会有问题，变成match_parent
    private int rowSpacing = 40;
    private int columnSpacing = 60;

    public LayoutTestView(Context context) {
        super(context);
    }

    public LayoutTestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LayoutTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int wantWidth = resolveSize(DEFAULT_WIDTH, widthMeasureSpec);
        int wantHeight = 0;
        int childLeft = paddingLeft;
        int childTop = paddingTop;
        int rowHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            ViewGroup.LayoutParams lp = childView.getLayoutParams();
            //结合父View的MeasureSpec和子类的LayoutParams来得出子View的MeasureSpec
            int childWidthMs = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, lp.width);
            int childHeightMs = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, lp.height);
            //测量子View以得出子类宽高
            measureChild(childView, childWidthMs, childHeightMs);
            rowHeight = Math.max(rowHeight, childView.getMeasuredHeight());
            if (childLeft + childView.getMeasuredWidth() + paddingRight > wantWidth) {
                childLeft = paddingLeft;
                childTop += rowHeight + rowSpacing;
                rowHeight = childView.getMeasuredHeight();
            }
            childLeft += childView.getMeasuredWidth() + columnSpacing;
        }
        wantHeight = childTop + rowSpacing + rowHeight + paddingBottom;
        wantHeight = resolveSize(DEFAULT_HEIGHT, wantHeight);
        setMeasuredDimension(wantWidth, wantHeight);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int padingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int width = right - left;
        int childLeft = getPaddingLeft();
        int childTop = getPaddingTop();
        int rowHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            rowHeight = Math.max(rowHeight, childView.getMeasuredHeight());
            if (childLeft + childView.getMeasuredWidth() + paddingRight > width) {
                childLeft = padingLeft;
                childTop = childTop + rowHeight + rowSpacing;
                rowHeight = childView.getMeasuredHeight();
            }
            childView.layout(childLeft, childTop,
                    childLeft + childView.getMeasuredWidth(), childTop + childView.getMeasuredHeight());
            childLeft += childView.getMeasuredWidth() + columnSpacing;
        }
    }

    public void setRowSpacing(int rowSpacing) {
        this.rowSpacing = rowSpacing;
        refresh();
    }


    public void setColumnSpacing(int columnSpacing) {
        this.columnSpacing = columnSpacing;
        refresh();
    }

    public int getRowSpacing() {
        return rowSpacing;
    }

    public int getColumnSpacing() {
        return columnSpacing;
    }

    private void refresh() {
        postInvalidate();
        requestLayout();
    }

}
